package cn.com.shadowless.xinning.presenter;


import android.util.Log;

import java.net.SocketTimeoutException;

import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.bill.BillCountInfo;
import cn.com.shadowless.xinning.model.bill.BillListInfo;
import cn.com.shadowless.xinning.net.HttpMethods;
import cn.com.shadowless.xinning.utils.Constants;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;


/**
 * The type Bill presenter.
 *
 * @author sHadowLess
 */
public class BillPresenter extends HttpMethods {

    private volatile static BillPresenter instance;

    private BillPresenter() {
    }

    /**
     * Gets singleton.
     *
     * @return the singleton
     */
    public static BillPresenter getSingleton() {
        if (instance == null) {
            synchronized (LoginPresenter.class) {
                if (instance == null) {
                    instance = new BillPresenter();
                }
            }
        }
        return instance;
    }

    /**
     * Gets bill info.
     *
     * @param userName        the user name
     * @param requestCallBack the request call back
     */
    public void getBillInfo(String userName, RequestCallBack<BillCountInfo> requestCallBack) {
        bill
                .billInfo(userName, userName)
                .compose(obsIoMain())
                .subscribe(new Observer<RequestResult<BillCountInfo>>() {
                    Disposable disposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(RequestResult<BillCountInfo> billCountInfoRequestResult) {
                        requestCallBack.success(billCountInfoRequestResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        requestCallBack.fail(exceptionMessage(checkException(e)));
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }

    /**
     * Gets bill list info.
     *
     * @param userName        the user name
     * @param requestCallBack the request call back
     */
    public void getBillListInfo(String userName, RequestCallBack<BillListInfo> requestCallBack) {
        bill
                .billListInfo(userName, userName)
                .compose(obsIoMain())
                .subscribe(new Observer<RequestResult<BillListInfo>>() {
                    Disposable disposable;

                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(RequestResult<BillListInfo> billListInfoRequestResult) {
                        requestCallBack.success(billListInfoRequestResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        requestCallBack.fail(exceptionMessage(checkException(e)));
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });
    }
}
